import 'package:flutter/material.dart';
import 'package:tianpu_edu/Classes/Home/Model/home_course_module_list.dart';
import 'package:tianpu_edu/Classes/libs/tool_text_utils.dart';

class HomeExpansionTileList extends StatefulWidget {

  final HomeCourseModuleList homeCourseModuleList;

  const HomeExpansionTileList({Key key, this.homeCourseModuleList}) : super(key: key);

  @override
  _HomeExpansionTileListState createState() => _HomeExpansionTileListState();
}

class _HomeExpansionTileListState extends State<HomeExpansionTileList> {

  List<int> mList = [];
  List<ExpanStateBean> expandStateList = [];

  // _HomeExpansionTileListState(){
  //   mList = [];
  //   expandStateList= [];
  // }

  _setCurrentIndex(int index,isExpand){
    setState(() {
      expandStateList.forEach((item) {
        if(item.index == index){
          item.isOpen =!isExpand;
        }
      });
    });
  }

  @override
  void initState() {
    super.initState();
    for(int i = 0; i < widget.homeCourseModuleList.data.length; i++){
      mList.add(i);
      expandStateList.add(ExpanStateBean(i,false,widget.homeCourseModuleList.data[i]));
    }
  }

  @override
  Widget build(BuildContext context) {
    if(mList.length <= 0){
      return Center(
        child: Container(padding: EdgeInsets.only(top: 100),
          child: Text('暂无课表'),
        ),
      );
    }
    return SingleChildScrollView(
      child: ExpansionPanelList(
        expandedHeaderPadding: EdgeInsets.all(0),
        elevation: 0,
        // 点击的回调函数
        expansionCallback: (index,bol){
          _setCurrentIndex(index,bol);
        },
        children: mList.map((index){
          Data _data = expandStateList[index].data;
          return ExpansionPanel(
              headerBuilder: (context,isExpanded){ //headerBuilder:头部   isExpanded是否打开
                return Container(//color: Colors.red,
                  // margin: EdgeInsets.all(13),
                  child: ListTile(
                    onTap: (){
                      _setCurrentIndex(index,expandStateList[index].isOpen);
                    },
                      title:Text(_data.courseModuleName)
                  ),
                );
              },
              body: Container(color: Colors.black12,
                // margin: EdgeInsets.all(10),
                child: Column(
                  children: _data.courseModuleBlockList.map((CourseModuleBlockList blockList) {
                    return Container(
                        width: screenSizeW(context),
                        child:Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(padding: EdgeInsets.symmetric(vertical: 12,horizontal: 20),
                                child: Text(blockList.courseModuleClassBlockName)),
                            Container(color: Colors.black,height: 0.5,),
                          ],
                        )
                    );
                  }).toList(),
                ),
              ),
              isExpanded: expandStateList[index].isOpen
          );
        }).toList(),
      ),
    );
  }
  Widget list (_data){
    return ListView.builder(itemCount: _data.courseModuleBlockList.length,
        itemBuilder: (BuildContext context, int index) {
          CourseModuleBlockList blockList = _data.courseModuleBlockList[index];
          return Container(child: Text(blockList.courseModuleClassBlockName),);
        });
  }
}

// 控制打开和闭合的类
class ExpanStateBean{
  bool isOpen;
  int index;
  Data data;
  ExpanStateBean(this.index,this.isOpen,this.data);
}